package com.vimi8.ebb.api.datareport;

import com.vimi8.ebb.configuration.HttpResponseEntity;
import com.vimi8.ebb.dto.*;
import com.vimi8.ebb.model.GeoCodings;
import com.vimi8.ebb.service.ElectricityBusinessService;
import com.vimi8.ebb.service.GeoCodingsService;
import com.vimi8.ebb.service.PorductInfoService;
import com.vimi8.ebb.util.CommonUtil;
import com.vimi8.ebb.util.SubStringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.synth.Region;
import java.lang.reflect.Array;
import java.util.*;

/**
 * Created by think on 2017/4/10.
 */
@RestController
@RequestMapping("/data/report")
public class TopAnalysisController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private GeoCodingsService geoCodingsService;
    @Autowired
    private PorductInfoService porductInfoService;
@Autowired
private ElectricityBusinessService electricityBusinessService;


    @ApiOperation(value = "查询数据报告店铺top分析(店铺不包括服务站点)",notes = "查询数据报告店铺top分析(店铺不包括服务站点)",response = String.class,tags = {"数据报告"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = String.class),
            @ApiResponse(code = 500, message = "失败,返回消息", response = String.class) })
    @RequestMapping(value = "/ebb/top/",method = RequestMethod.GET)
    public HttpResponseEntity<HashMap<String,List<RegionManamentDto>>> getEbbRegionManamentDto(@ApiParam(value = "year",required = true) @RequestParam(value="year",required = true)int year,
                                                                                                            @ApiParam(value = "quarter",required = true) @RequestParam(value="quarter",required = true)int quarter,
                                                                                                            @ApiParam(value = "topType",required = true) @RequestParam(value="topType",required = true)int topType) {
        HashMap<String,List<RegionManamentDto>> topAnaizeMap=new HashMap<String,List<RegionManamentDto>>();
        String firstMonth=null;
        String secondMonth=null;
        String thirdMonth=null;

        if(1==quarter){
            firstMonth=year+"-01";
            secondMonth=year+"-02";
            thirdMonth=year+"-03";
        }
        if(2==quarter){
            firstMonth=year+"-04";
            secondMonth=year+"-05";
            thirdMonth=year+"-06";
        }
        if(3==quarter){
            firstMonth=year+"-07";
            secondMonth=year+"-08";
            thirdMonth=year+"-09";
        }
        if(4==quarter){
            firstMonth=year+"-10";
            secondMonth=year+"-11";
            thirdMonth=year+"-12";
        }
        // 碧江区 1月份 top10出现的产品数量  出现数量/碧江区总数
        // 碧江区 2月份 top10出现的产品数量  出现数量/碧江区总数
        // 碧江区 3月份 top10出现的产品数量  出现数量/碧江区总数

        // 高薪区 1月份 top10出现的产品数量  出现数量/碧江区总数
        // 高薪区 2月份 top10出现的产品数量  出现数量/碧江区总数
        // 高薪区 3月份 top10出现的产品数量  出现数量/碧江区总数
        try {
            // getTopStatics
            //  for(GeoCodings geoCodings:geoCodingses){
            List<RegionManamentDto> firstTotalDtos=    electricityBusinessService.getStoreSalesTop(null,firstMonth,null,topType);
            List<RegionManamentDto> secondTotalDtos=    electricityBusinessService.getStoreSalesTop(null,secondMonth,null,topType);
            List<RegionManamentDto> thirdTotalDtos=    electricityBusinessService.getStoreSalesTop(null,thirdMonth,null,topType);

            topAnaizeMap.put(firstMonth,firstTotalDtos);
            topAnaizeMap.put(secondMonth,secondTotalDtos);
            topAnaizeMap.put(thirdMonth,thirdTotalDtos);
            // }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new HttpResponseEntity(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<HashMap<String,List<RegionManamentDto>>>(topAnaizeMap, HttpStatus.OK);
    }



    @ApiOperation(value = "查询数据报告店铺top占比分析(店铺不包括服务站点)",notes = "查询数据报告店铺top占比分析(店铺不包括服务站点)",response = String.class,tags = {"数据报告"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = String.class),
            @ApiResponse(code = 500, message = "失败,返回消息", response = String.class) })
    @RequestMapping(value = "/ebb/top/rate/",method = RequestMethod.GET)
    public HttpResponseEntity<HashMap<String,List<TopAnalysisDto>>> getEbbTopAnalysisDto(@ApiParam(value = "year",required = true) @RequestParam(value="year",required = true)int year,
                                                         @ApiParam(value = "quarter",required = true) @RequestParam(value="quarter",required = true)int quarter,
                                                         @ApiParam(value = "topType",required = true) @RequestParam(value="topType",required = true)int topType) {
        HashMap<String,List<TopAnalysisDto>> topAnaizeMap=new HashMap<String,List<TopAnalysisDto>>();
        String firstMonth=null;
        String secondMonth=null;
        String thirdMonth=null;
        String geoCode = SubStringUtil.separateGeoCode("520600000000","3");
        logger.info("======================================code:" + geoCode);
        List<GeoCodings> geoCodingses = geoCodingsService.getGeoCodingsByCode("520600000000",geoCode);

        if(1==quarter){
            firstMonth=year+"-01";
            secondMonth=year+"-02";
            thirdMonth=year+"-03";
        }
        if(2==quarter){
            firstMonth=year+"-04";
            secondMonth=year+"-05";
            thirdMonth=year+"-06";
        }
        if(3==quarter){
            firstMonth=year+"-07";
            secondMonth=year+"-08";
            thirdMonth=year+"-09";
        }
        if(4==quarter){
            firstMonth=year+"-10";
            secondMonth=year+"-11";
            thirdMonth=year+"-12";
        }
        // 碧江区 1月份 top10出现的店铺数量  出现数量/碧江区总数
        // 碧江区 2月份 top10出现的店铺数量  出现数量/碧江区总数
        // 碧江区 3月份 top10出现的店铺数量  出现数量/碧江区总数

        // 高薪区 1月份 top10出现的店铺数量  出现数量/碧江区总数
        // 高薪区 2月份 top10出现的店铺数量  出现数量/碧江区总数
        // 高薪区 3月份 top10出现的店铺数量  出现数量/碧江区总数
        try {
//            List<TopAnalysisDto> topAnaizList=new ArrayList<TopAnalysisDto>();
//            List<TopAnalysisDto> firstTopAnalysisDto=  electricityBusinessService.getTopAnalysisDtoTop(null,firstMonth,null,topType);
//            List<TopAnalysisDto> secondTopAnalysisDto=  electricityBusinessService.getTopAnalysisDtoTop(null,secondMonth,null,topType);
//            List<TopAnalysisDto> thirdTopAnalysisDto=  electricityBusinessService.getTopAnalysisDtoTop(null,thirdMonth,null,topType);
//            int fistMonthTotal=topType;//electricityBusinessService.getElectricityBusinessCountByCode(null,firstMonth);
//            int secondMonthTotal=topType;//electricityBusinessService.getElectricityBusinessCountByCode(null,secondMonth);
//            int thirdMonthTotal=topType;
//            obtainTopAnalysis(firstMonth, firstTopAnalysisDto, fistMonthTotal);
//            obtainTopAnalysis(secondMonth, secondTopAnalysisDto, fistMonthTotal);
//            obtainTopAnalysis(thirdMonth, thirdTopAnalysisDto, fistMonthTotal);
//            topAnaizList.addAll(firstTopAnalysisDto);
//            topAnaizList.addAll(secondTopAnalysisDto);
//            topAnaizList.addAll(thirdTopAnalysisDto);
//
//            for(TopAnalysisDto topAnalysisDto:topAnaizList){
//              for(GeoCodings geoCodings:geoCodingses){
//                  if(topAnalysisDto.getAddressCode().equals(geoCodings.getId())){
//
//                  }
//              }
//
//            }

            for(GeoCodings geoCodings:geoCodingses){
                List<TopAnalysisDto> topAnaizList=new ArrayList<TopAnalysisDto>();
//                List<RegionManamentDto> firstTotalDtos=    electricityBusinessService.getStoreSalesTop(geoCodings.getId(),firstMonth,null,topType);
//                List<RegionManamentDto> secondTotalDtos=    electricityBusinessService.getStoreSalesTop(geoCodings.getId(),secondMonth,null,topType);
//                List<RegionManamentDto> thirdTotalDtos=    electricityBusinessService.getStoreSalesTop(geoCodings.getId(),thirdMonth,null,topType);
//
                int fistMonthTotal=topType;//electricityBusinessService.getElectricityBusinessCountByCode(null,firstMonth);
                int secondMonthTotal=topType;//electricityBusinessService.getElectricityBusinessCountByCode(null,secondMonth);
                int thirdMonthTotal=topType;//electricityBusinessService.getElectricityBusinessCountByCode(null,thirdMonth);
                TopAnalysisDto firstTopAnalysis=new TopAnalysisDto();
                TopAnalysisDto secondTopAnalysis=new TopAnalysisDto();
                TopAnalysisDto thirdTopAnalysis=new TopAnalysisDto();
                firstTopAnalysis=  electricityBusinessService.getTopAnalysisDtoTop(geoCodings.getId(),firstMonth,null,topType);
                secondTopAnalysis=  electricityBusinessService.getTopAnalysisDtoTop(geoCodings.getId(),secondMonth,null,topType);
                thirdTopAnalysis=  electricityBusinessService.getTopAnalysisDtoTop(geoCodings.getId(),thirdMonth,null,topType);
                logger.info("firstTopAnalysis="+firstTopAnalysis);
                logger.info("secondTopAnalysis="+secondTopAnalysis);
                logger.info("thirdTopAnalysis="+thirdTopAnalysis);
                logger.info("fistMonthTotal="+fistMonthTotal+" secondMonthTotal="+secondMonthTotal+" thirdMonthTotal="+thirdMonthTotal);

              // firstTopAnalysis.setCurrentTotal(firstTotalDtos.size());
                Double firstRate=0.00;
                if(firstTopAnalysis!=null&&fistMonthTotal>0){
                     firstRate=  ((firstTopAnalysis.getCurrentTotal()*1.0)/fistMonthTotal)*100;
                    String firstRateStr= CommonUtil.parseDecimal(firstRate);
                    firstTopAnalysis.setRate(firstRateStr+"%");
                }else{
                     firstTopAnalysis=new TopAnalysisDto();
                }


                firstTopAnalysis.setMonth(firstMonth.substring(firstMonth.lastIndexOf("-")+1,firstMonth.length()));


               // secondTopAnalysis.setCurrentTotal(secondTotalDtos.size());
                Double secondRate=  0.00;
                if(null!=secondTopAnalysis&&secondMonthTotal>0){
                    secondRate=  ((secondTopAnalysis.getCurrentTotal()*1.0)/secondMonthTotal)*100;
                    String secondRateStr= CommonUtil.parseDecimal(secondRate);
                    secondTopAnalysis.setRate(secondRateStr+"%");
                }else{
                    secondTopAnalysis=new TopAnalysisDto();
                }


                secondTopAnalysis.setMonth(secondMonth.substring(secondMonth.lastIndexOf("-")+1,secondMonth.length()));


               // thirdTopAnalysis.setCurrentTotal(thirdTotalDtos.size());
                Double thirdRate=0.00;
                if(null!=thirdTopAnalysis&&thirdMonthTotal>0){
                    thirdRate=  ((thirdTopAnalysis.getCurrentTotal()*1.0)/thirdMonthTotal)*100;
                    String thirdRateStr= CommonUtil.parseDecimal(thirdRate);
                    thirdTopAnalysis.setRate(thirdRateStr+"%");
                }else{
                    thirdTopAnalysis=new TopAnalysisDto();
                }

                thirdTopAnalysis.setMonth(thirdMonth.substring(thirdMonth.lastIndexOf("-")+1,thirdMonth.length()));

                topAnaizList.add(firstTopAnalysis);
                topAnaizList.add(secondTopAnalysis);
                topAnaizList.add(thirdTopAnalysis);
                topAnaizeMap.put(geoCodings.getGeoName(),topAnaizList);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new HttpResponseEntity(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<HashMap<String,List<TopAnalysisDto>>>(topAnaizeMap, HttpStatus.OK);
    }

    private void obtainTopAnalysis(String firstMonth, List<TopAnalysisDto> firstTopAnalysisDto, int fistMonthTotal) {
        for(TopAnalysisDto topAnalysisDto:firstTopAnalysisDto){
          //  if(geoCodings.getId().equals(topAnalysisDto.getAddressCode())){
                Double firstRate=0.00;
                if(fistMonthTotal>0){
                    firstRate=  ((topAnalysisDto.getCurrentTotal()*1.0)/fistMonthTotal)*100;
                }

                String firstRateStr= CommonUtil.parseDecimal(firstRate);
                topAnalysisDto.setRate(firstRateStr+"%");
                topAnalysisDto.setMonth(firstMonth.substring(firstMonth.lastIndexOf("-")+1,firstMonth.length()));

            //}

        }
    }


    @ApiOperation(value = "查询数据报告产品top分析统计信息",notes = "查询数据报告产品top分析统计信息",response = String.class,tags = {"数据报告"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = String.class),
            @ApiResponse(code = 500, message = "失败,返回消息", response = String.class) })
    @RequestMapping(value = "/product/top/",method = RequestMethod.GET)
    public HttpResponseEntity<HashMap<String,List<ProductTradingBusinessDto>>> getProductTradingBusinessDto(@ApiParam(value = "year",required = true) @RequestParam(value="year",required = true)int year,
                                                                                             @ApiParam(value = "quarter",required = true) @RequestParam(value="quarter",required = true)int quarter,
                                                                                             @ApiParam(value = "topType",required = true) @RequestParam(value="topType",required = true)int topType) {
        HashMap<String,List<ProductTradingBusinessDto>> topAnaizeMap=new HashMap<String,List<ProductTradingBusinessDto>>();
        String firstMonth=null;
        String secondMonth=null;
        String thirdMonth=null;

        if(1==quarter){
            firstMonth=year+"-01";
            secondMonth=year+"-02";
            thirdMonth=year+"-03";
        }
        if(2==quarter){
            firstMonth=year+"-04";
            secondMonth=year+"-05";
            thirdMonth=year+"-06";
        }
        if(3==quarter){
            firstMonth=year+"-07";
            secondMonth=year+"-08";
            thirdMonth=year+"-09";
        }
        if(4==quarter){
            firstMonth=year+"-10";
            secondMonth=year+"-11";
            thirdMonth=year+"-12";
        }
        // 碧江区 1月份 top10出现的产品数量  出现数量/碧江区总数
        // 碧江区 2月份 top10出现的产品数量  出现数量/碧江区总数
        // 碧江区 3月份 top10出现的产品数量  出现数量/碧江区总数

        // 高薪区 1月份 top10出现的产品数量  出现数量/碧江区总数
        // 高薪区 2月份 top10出现的产品数量  出现数量/碧江区总数
        // 高薪区 3月份 top10出现的产品数量  出现数量/碧江区总数
        try {
            // getTopStatics
          //  for(GeoCodings geoCodings:geoCodingses){
                List<TopAnalysisDto> topAnaizList=new ArrayList<TopAnalysisDto>();
                List<ProductTradingBusinessDto>  firstPdto=   porductInfoService.getTopStatics(null,firstMonth,topType);
                List<ProductTradingBusinessDto>  secondPdto=   porductInfoService.getTopStatics(null,secondMonth,topType);
                List<ProductTradingBusinessDto>  thirdPdto=   porductInfoService.getTopStatics(null,thirdMonth,topType);

                topAnaizeMap.put(firstMonth,firstPdto);
            topAnaizeMap.put(secondMonth,secondPdto);
            topAnaizeMap.put(thirdMonth,thirdPdto);
           // }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new HttpResponseEntity(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<HashMap<String,List<ProductTradingBusinessDto>>>(topAnaizeMap, HttpStatus.OK);
    }


    @ApiOperation(value = "查询数据报告产品top占比分析",notes = "查询数据报告产品top占比分析",response = String.class,tags = {"数据报告"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = String.class),
            @ApiResponse(code = 500, message = "失败,返回消息", response = String.class) })
    @RequestMapping(value = "/product/top/rate/",method = RequestMethod.GET)
    public HttpResponseEntity<HashMap<String,List<TopAnalysisDto>>> getProductTopAnalysisDto(@ApiParam(value = "year",required = true) @RequestParam(value="year",required = true)int year,
                                                                                      @ApiParam(value = "quarter",required = true) @RequestParam(value="quarter",required = true)int quarter,
                                                                                      @ApiParam(value = "topType",required = true) @RequestParam(value="topType",required = true)int topType) {
        HashMap<String,List<TopAnalysisDto>> topAnaizeMap=new HashMap<String,List<TopAnalysisDto>>();
        String firstMonth=null;
        String secondMonth=null;
        String thirdMonth=null;
        String geoCode = SubStringUtil.separateGeoCode("520600000000","3");
        logger.info("======================================code:" + geoCode);
        List<GeoCodings> geoCodingses = geoCodingsService.getGeoCodingsByCode("520600000000",geoCode);

        if(1==quarter){
            firstMonth=year+"-01";
            secondMonth=year+"-02";
            thirdMonth=year+"-03";
        }
        if(2==quarter){
            firstMonth=year+"-04";
            secondMonth=year+"-05";
            thirdMonth=year+"-06";
        }
        if(3==quarter){
            firstMonth=year+"-07";
            secondMonth=year+"-08";
            thirdMonth=year+"-09";
        }
        if(4==quarter){
            firstMonth=year+"-10";
            secondMonth=year+"-11";
            thirdMonth=year+"-12";
        }
        // 碧江区 1月份 top10出现的产品数量  出现数量/碧江区总数
        // 碧江区 2月份 top10出现的产品数量  出现数量/碧江区总数
        // 碧江区 3月份 top10出现的产品数量  出现数量/碧江区总数

        // 高薪区 1月份 top10出现的产品数量  出现数量/碧江区总数
        // 高薪区 2月份 top10出现的产品数量  出现数量/碧江区总数
        // 高薪区 3月份 top10出现的产品数量  出现数量/碧江区总数
        try {
            // getTopStatics
            for(GeoCodings geoCodings:geoCodingses){
                List<TopAnalysisDto> topAnaizList=new ArrayList<TopAnalysisDto>();
                List<ProductTradingBusinessDto>  firstPdto=   porductInfoService.getTopStatics(geoCodings.getId(),firstMonth,topType);
                List<ProductTradingBusinessDto>  secondPdto=   porductInfoService.getTopStatics(geoCodings.getId(),secondMonth,topType);
                List<ProductTradingBusinessDto>  thirdPdto=   porductInfoService.getTopStatics(geoCodings.getId(),thirdMonth,topType);

                int fistMonthTotal=topType;//porductInfoService.getTopStaticsCount(null,firstMonth);
                int secondMonthTotal=topType;//porductInfoService.getTopStaticsCount(null,secondMonth);
                int thirdMonthTotal=topType;//porductInfoService.getTopStaticsCount(null,thirdMonth);

                logger.info("fistMonthTotal="+fistMonthTotal+" secondMonthTotal="+secondMonthTotal+" thirdMonthTotal="+thirdMonthTotal);
                TopAnalysisDto firstTopAnalysis=new TopAnalysisDto();
                firstTopAnalysis.setCurrentTotal(firstPdto.size());
                Double firstRate=  0.00;
                if(fistMonthTotal>0){
                     firstRate=  ((firstPdto.size()*1.0)/fistMonthTotal)*100;
                }

                String firstRateStr= CommonUtil.parseDecimal(firstRate);
                firstTopAnalysis.setRate(firstRateStr+"%");
                firstTopAnalysis.setMonth(firstMonth.substring(firstMonth.lastIndexOf("-")+1,firstMonth.length()));

                TopAnalysisDto secondTopAnalysis=new TopAnalysisDto();
                secondTopAnalysis.setCurrentTotal(secondPdto.size());
                Double secondRate=  0.00;
                if(secondMonthTotal>0){
                    secondRate=  ((secondPdto.size()*1.0)/secondMonthTotal)*100;
                }
                String secondRateStr= CommonUtil.parseDecimal(secondRate);
                secondTopAnalysis.setRate(secondRateStr+"%");
                secondTopAnalysis.setMonth(secondMonth.substring(secondMonth.lastIndexOf("-")+1,secondMonth.length()));
                TopAnalysisDto thirdTopAnalysis=new TopAnalysisDto();
                thirdTopAnalysis.setCurrentTotal(thirdPdto.size());
                Double thirdRate=  0.00;
                if(thirdMonthTotal>0){
                    thirdRate=  ((thirdPdto.size()*1.0)/thirdMonthTotal)*100;
                }
                String thirdRateStr= CommonUtil.parseDecimal(thirdRate);
                thirdTopAnalysis.setRate(thirdRateStr+"%");
                thirdTopAnalysis.setMonth(thirdMonth.substring(thirdMonth.lastIndexOf("-")+1,thirdMonth.length()));

                topAnaizList.add(firstTopAnalysis);
                topAnaizList.add(secondTopAnalysis);
                topAnaizList.add(thirdTopAnalysis);
                topAnaizeMap.put(geoCodings.getGeoName(),topAnaizList);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new HttpResponseEntity(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<HashMap<String,List<TopAnalysisDto>>>(topAnaizeMap, HttpStatus.OK);
    }

//  6.该季度连续三个月销量在TOP10榜单中的店铺及交易情况分析。（横轴为店铺名称，左纵轴为交易量，右纵轴为交易金额）
//  三个月在top10中都出现
    // 每个月的 展示 店铺名字、交易量、交易额

    @ApiOperation(value = "连续三个月销量在TOP10榜单中的店铺及交易情况分析。（横轴为店铺名称，左纵轴为交易量，右纵轴为交易金额）",notes = "连续三个月销量在TOP10榜单中的店铺及交易情况分析。（横轴为店铺名称，左纵轴为交易量，右纵轴为交易金额）",response = String.class,tags = {"数据报告"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = String.class),
            @ApiResponse(code = 500, message = "失败,返回消息", response = String.class) })
    @RequestMapping(value = "/ebb/continuity/top/",method = RequestMethod.GET)
    public HttpResponseEntity<HashMap<String,List<RegionManamentDto>>> getContinuityTrading(@ApiParam(value = "year",required = true) @RequestParam(value="year",required = true)int year,
                                                                                                            @ApiParam(value = "quarter",required = true) @RequestParam(value="quarter",required = true)int quarter,
                                                                                                            @ApiParam(value = "topType",required = true) @RequestParam(value="topType",required = true)int topType) {
        HashMap<String,List<RegionManamentDto>> topAnaizeMap=new HashMap<String,List<RegionManamentDto>>();
        String firstMonth=null;
        String secondMonth=null;
        String thirdMonth=null;

        if(1==quarter){
            firstMonth=year+"-01";
            secondMonth=year+"-02";
            thirdMonth=year+"-03";
        }
        if(2==quarter){
            firstMonth=year+"-04";
            secondMonth=year+"-05";
            thirdMonth=year+"-06";
        }
        if(3==quarter){
            firstMonth=year+"-07";
            secondMonth=year+"-08";
            thirdMonth=year+"-09";
        }
        if(4==quarter){
            firstMonth=year+"-10";
            secondMonth=year+"-11";
            thirdMonth=year+"-12";
        }
        // 碧江区 1月份 top10出现的产品数量  出现数量/碧江区总数
        // 碧江区 2月份 top10出现的产品数量  出现数量/碧江区总数
        // 碧江区 3月份 top10出现的产品数量  出现数量/碧江区总数

        // 高薪区 1月份 top10出现的产品数量  出现数量/碧江区总数
        // 高薪区 2月份 top10出现的产品数量  出现数量/碧江区总数
        // 高薪区 3月份 top10出现的产品数量  出现数量/碧江区总数
       Map<String,RegionManamentDto> regionManamentDtoMap=new HashMap<String,RegionManamentDto>();
        try {
            List<RegionManamentDto> firstTotalDtos=    electricityBusinessService.getStoreSalesTop(null,firstMonth,null,topType);
            List<RegionManamentDto> secondTotalDtos=    electricityBusinessService.getStoreSalesTop(null,secondMonth,null,topType);
            List<RegionManamentDto> thirdTotalDtos=    electricityBusinessService.getStoreSalesTop(null,thirdMonth,null,topType);
            logger.info("firstTotalDtos="+firstTotalDtos);
            logger.info("secondTotalDtos="+secondTotalDtos);
            logger.info("thirdTotalDtos="+thirdTotalDtos);
            List<RegionManamentDto> allDto=new ArrayList<>();
            allDto.addAll(firstTotalDtos);
            allDto.addAll(secondTotalDtos);
            allDto.addAll(thirdTotalDtos);

            //获取连续三个月都在top中的店铺
            Collection<RegionManamentDto> continueRegionDto=   getRepeatRedionDto(allDto);
            logger.info("continueRegionDto="+continueRegionDto);
            firstTotalDtos=getRegionDto(continueRegionDto,firstTotalDtos);
            secondTotalDtos=getRegionDto(continueRegionDto,secondTotalDtos);
            thirdTotalDtos=getRegionDto(continueRegionDto,thirdTotalDtos);


            topAnaizeMap.put(firstMonth,firstTotalDtos);
            topAnaizeMap.put(secondMonth,secondTotalDtos);
            topAnaizeMap.put(thirdMonth,thirdTotalDtos);
            // }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new HttpResponseEntity(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<HashMap<String,List<RegionManamentDto>>>(topAnaizeMap, HttpStatus.OK);
    }
private void putRegionDto(List<RegionManamentDto> regionManamentDtos,Map<String,RegionManamentDto> regionManamentDtoMap){
    for(RegionManamentDto regionManamentDto:regionManamentDtos){
        regionManamentDtoMap.put(regionManamentDto.getStoreName(),regionManamentDto);
    }
}
    private Collection<RegionManamentDto> getRepeatRedionDto(List<RegionManamentDto> regionManamentDtos){
        List<RegionManamentDto> regionManamentDtosResult=new ArrayList<>();
        Map<String,RegionManamentDto> regionManamentDtoMap=new HashMap<>();
        for(RegionManamentDto regionManamentDto:regionManamentDtos){
            int count=0;
            for(RegionManamentDto regionManamentDto1:regionManamentDtos){
                if(regionManamentDto.getStoreName().equals(regionManamentDto1.getStoreName())){
                    count++;
                    if(count>=3){
                        regionManamentDtoMap.put(regionManamentDto.getStoreName(),regionManamentDto1);
                    }
                }
            }
        }
        return regionManamentDtoMap.values();
    }
    private List<RegionManamentDto> getRegionDto( Collection<RegionManamentDto> regionManamentDtoCollection,List<RegionManamentDto> regionManamentDtos){
        List<RegionManamentDto> regionManamentDtoList=new ArrayList<RegionManamentDto>();
        for(RegionManamentDto regionManamentDto:regionManamentDtoCollection){
            for(RegionManamentDto regionManamentDto1:regionManamentDtos){
                if(regionManamentDto!=null&&regionManamentDto.getStoreName().equals(regionManamentDto1.getStoreName()))
                    regionManamentDtoList.add(regionManamentDto);
               continue;
            }
        }

        return regionManamentDtoList;

    }

}
